package com.yogee.topseller.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.text.Html;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.yogee.topseller.R;
import com.yogee.topseller.activity.PayActivity;
import com.yogee.topseller.bean.GbbdBean;
import com.yogee.topseller.bean.SxrBean;
import com.yogee.topseller.utils.LogUtils;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import z.collapsedtextviewlibrary.CollapsedTextView;

/**
 * @auathor ${XiaoJianghong}
 * @date 2017/6/26.
 */

public class SxrAdapter extends RecyclerView.Adapter<SxrAdapter.ItemHolder> {

    private Context context;
    private List<SxrBean.Item> beans = new ArrayList<>();


    private PayActivity.Listener listener;

    public SxrAdapter(Context context) {
        this.context = context;
    }

    public void addData(List<SxrBean.Item> beans) {
        this.beans = beans;
        notifyDataSetChanged();
    }

    public void setListener(PayActivity.Listener listener){
        this.listener = listener;
    }

    @Override
    public ItemHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_detail_sxr,null);
        return new ItemHolder(view);
    }

    @Override
    public void onBindViewHolder(ItemHolder holder, final int position) {
        SxrBean.Item bean = beans.get(position);
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");

        holder.tv_time.setText(sdf.format(new Date(Long.parseLong(String.valueOf(bean.getPublishdate())))));
        holder.tv_value1.setText(bean.getIname());
        holder.tv_value2.setText(bean.getBusinessentity());
        holder.tv_value3.setText(bean.getGistid());
        holder.tv_value4.setText(bean.getAreaname());
        holder.tv_value5.setText(bean.getCardnum());
        holder.tv_value6.setText(bean.getCourtname());
        if(bean.getType()==1)
            holder.tv_value7.setText("公司");
        else
            holder.tv_value7.setText("个人");

        holder.tv_value8.setText(bean.getGistunit());
        holder.tv_value9.setText(bean.getPerformance());
        holder.tv_value10.setText(sdf.format(new Date(Long.parseLong(String.valueOf(bean.getRegdate())))));
        holder.tv_value11.setText(bean.getCasecode());

//        LogUtils.e("失信人","shixinren"+position+bean.getDuty());
        final TextView tv1 = holder.tv_detail1;
        final TextView tv2 = holder.tv_detail2;
        final TextView tv_btn1 = holder.tv_btn;
        final TextView tv_btn2 = holder.tv_btn1;
        if(TextUtils.isEmpty(bean.getDuty())){
            holder.ll1.setVisibility(View.GONE);
        }else{
            holder.ll1.setVisibility(View.VISIBLE);
            tv1.setText(Html.fromHtml(bean.getDuty()));
        }
        if(TextUtils.isEmpty(bean.getDisrupttypename())){
            holder.ll2.setVisibility(View.GONE);
        }else{
            holder.ll2.setVisibility(View.VISIBLE);
            tv2.setText(bean.getDisrupttypename());
        }
        tv1.post(new Runnable() {
            @Override
            public void run() {
                //获取textView的行数
                int txtPart = tv1.getLineCount();
                if(txtPart>2){
                    tv1.setMaxLines(2);
                    tv_btn1.setText("展开");
                    tv_btn1.setVisibility(View.VISIBLE);
                }else{
                    tv_btn1.setVisibility(View.GONE);
                }
            }
        });
        tv2.post(new Runnable() {
            @Override
            public void run() {
                //获取textView的行数
                int txtPart = tv2.getLineCount();
                if(txtPart>2){
                    tv2.setMaxLines(2);
                    tv_btn2.setText("展开");
                    tv_btn2.setVisibility(View.VISIBLE);
                }else{
                    tv_btn2.setVisibility(View.GONE);
                }
            }
        });
        tv_btn1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(tv_btn1.getText().toString().equals("展开")){
                    tv1.setMaxLines(Integer.MAX_VALUE);  //Integer.MAX_VALUE
                    tv_btn1.setText("收起");
                }else{
                    tv1.setMaxLines(2);
                    tv_btn1.setText("展开");
                }
            }
        });
        tv_btn2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(tv_btn2.getText().toString().equals("展开")){
                    tv2.setMaxLines(Integer.MAX_VALUE);  //Integer.MAX_VALUE
                    tv_btn2.setText("收起");
                }else{
                    tv2.setMaxLines(2);
                    tv_btn2.setText("展开");
                }
            }
        });

    }

    @Override
    public int getItemCount() {
        return beans == null ? 0 : beans.size();
    }

    class ItemHolder extends RecyclerView.ViewHolder {

        @BindView(R.id.tv_time)
        TextView tv_time;
        @BindView(R.id.tv_value1)
        TextView tv_value1;
        @BindView(R.id.tv_value2)
        TextView tv_value2;
        @BindView(R.id.tv_value3)
        TextView tv_value3;
        @BindView(R.id.tv_value4)
        TextView tv_value4;
        @BindView(R.id.tv_value5)
        TextView tv_value5;
        @BindView(R.id.tv_value6)
        TextView tv_value6;
        @BindView(R.id.tv_value7)
        TextView tv_value7;
        @BindView(R.id.tv_value8)
        TextView tv_value8;
        @BindView(R.id.tv_value9)
        TextView tv_value9;
        @BindView(R.id.tv_value10)
        TextView tv_value10;
        @BindView(R.id.tv_value11)
        TextView tv_value11;
        @BindView(R.id.ll1)
        LinearLayout ll1;
        @BindView(R.id.ll2)
        LinearLayout ll2;
        @BindView(R.id.tv_detail1)
        TextView tv_detail1;
        @BindView(R.id.tv_btn)
        TextView tv_btn;

        @BindView(R.id.tv_detail2)
        TextView tv_detail2;
        @BindView(R.id.tv_btn1)
        TextView tv_btn1;
        public ItemHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }


}